package com.afkt.project.slice;


import com.afkt.project.model.item.ButtonList;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.adapter.ButtonAdapter;
import com.afkt.project.util.SkipUtils;
import com.dev.base.ability.MVPAbilitySlice;
import com.dev.base.ability.MVVMAbilitySlice;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class FrameworkSlice extends AbilitySlice {


    private DirectionalLayout bar_layout;

    private Image ui_return;

    private DirectionalLayout ui_bar;

    private ListContainer UI_list;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_framework);


        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_bar_layout);
        ui_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ui_bar, null, false);
        Text text = (Text) ui_bar.findComponentById(ResourceTable.Id_title_name);
        text.setText("FrameWork 框架");
        bar_layout.addComponent(ui_bar);
        UI_list = (ListContainer) findComponentById(ResourceTable.Id_UI_List);
        ui_return = (Image) ui_bar.findComponentById(ResourceTable.Id_ui_return);
        ui_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        ButtonAdapter buttonAdapter = new ButtonAdapter(ButtonList.getModuleFrameworkButtonValues(), this);
        UI_list.setItemProvider(buttonAdapter);
        UI_list.setItemClickedListener((listContainer, component, i, l) -> {
            ButtonValue buttonValue = (ButtonValue) UI_list.getItemProvider().getItem(i);
            switch (buttonValue.type) {
                case ButtonValue.BTN_MVP: // MVP
                    SkipUtils.startActivity(new MVPAbilitySlice(), buttonValue);
                    break;
                case ButtonValue.BTN_MVVM: // MVVM
                    SkipUtils.startActivity(new MVVMAbilitySlice(), buttonValue);
                    break;

            }
        });

    }




    @Override
    public void onActive() {
        super.onActive();
        if (bar_layout.getChildCount() == 0) {
            bar_layout.addComponent(ui_bar);
        }
    }

    @Override
    protected void onBackground() {
        //bar_layout.removeComponent(ui_bar);
        super.onBackground();
        bar_layout.removeAllComponents();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
